<div class="ibox float-e-margins">
    <div class="ibox-title" style="border: none;">
        <h5 style="color: #00afef"><i>Add</i></h5>
        <div id="form-add-btn" class="ibox-tools">
            <button class="btn btn-sm btn-primary save-btn-tab" id="saveProg">Save</button>
            <button class="btn btn-sm btn-primary btn-outline close-add cancel-btn-tab">Cancel</button>
        </div>
    </div>
    <div class="ibox-content">
        <form class="form-horizontal" id="progForm">
            <div class="form-group">
                <label class="col-lg-4 control-label">Code</label>
                <div class="col-lg-3">
                    <input type="text" name="pcode" class="form-control">
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-4 control-label">Name</label>
                <div class="col-lg-5">
                    <input type="text" name="pname" class="form-control">
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-4 control-label">Description</label>
                <div class="col-lg-5">
                    <input type="text" name="desc" class="form-control">
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-4 control-label">Module</label>
                <div class="col-lg-5">
                   <select class="form-control chosen" name="modID" id ="moduleIdentify">
                   <option value="">--</option>
                   <?php foreach($mods as $m) { ?>
                        <option value ="<?php echo $m->module_id;?>"><?php echo $m->module_name; ?></option>
                   <?php } ?>
                   </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-4 control-label">Program Group</label>
                <div class="col-lg-5">
                    <select class="form-control chosen" name="ppGroup" id = "module_menu_option">
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-4 control-label" >Type</label>
                <div class="col-lg-5">
                    <select class="form-control chosen" name="pType">
                        <?php foreach($prog_type as $p): ?>
                        <option value="<?php echo $p->lookup_code; ?>"><?php echo $p->meaning; ?></option>
                        <?php endforeach ?>
                    </select>
                </div>
            </div>
           <div class="form-group">
                <label class="col-lg-4 control-label">Executable Filename</label>
                <div class="col-lg-5">
                     <input type="text" name="exename" class="form-control">
                </div>
            </div>
            <div class="form-group">
            <label class="col-lg-4 control-label">Enable</label>
                <div class="col-lg-1">
                    <input type="checkbox" checked class="i-checks" value="ok" name="enabled" id="ena">
                </div>
            </div>
        </form>
    </div>
    <div class="ibox-content">
        <form id="prog_parama">
            <table class="table dataTables table-bordered">
                <thead>
                    <tr>
                        <th style="width: 80px">Seq. No.</th>
                        <th>Name</th>
                        <th>Description</th>
                        <th>Data Type</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <input type="hidden" value="" name="program_id">
                        <td style="margin:0px;padding:0px;">
                            <input name="seq_no" type="text" class="form-control-custom">
                        </td>
                        <td style="margin:0px;padding:0px;">
                            <input name="name" type="text" class="form-control-custom">
                        </td>
                        <td style="margin:0px;padding:0px;">
                            <input name="desc" type="text" class="form-control-custom">
                        </td>
                        <td style="margin:0px;padding:0px;">
                            <select name="data_type" class="form-control chosen-select" name="" id="">
                                <option value="">--</option>
                                <?php foreach($data_type as $d): ?>
                                <option value="<?php echo $d->lookup_code; ?>"><?php echo $d->meaning; ?></option>
                            <?php endforeach; ?>
                            </select>
                        </td>
                        <td style="margin:0px;padding:0px;">
                            <button type="button" style="width:100%;height: 29px;" class="btn btn-white btn-md" id="saveparama"><i class="fa fa-plus success "></i></button>
                        </td>
                    </tr>
                </tbody>
                <tbody id="prog_paraam_tbl">
                    
                </tbody>
            </table>
        </form>
    </div>
</div>
<script>
    $(document).ready(function(){
        $('#prog_parama').hide();
        $('.chosen').chosen({width: "100%"});
        $(".chosen-select").chosen({disable_search_threshold: 10,width: '100%'});
        //programlist();
        $('.i-checks').iCheck({
            checkboxClass: 'icheckbox_square-green',
            radioClass: 'iradio_square-green'
        });
        $('#moduleIdentify').change(function(){
            console.log($('#ena').val());
            programlist();
        });

        function programlist(){
            var mis = $('#moduleIdentify').val();
            $.ajax({
            url: "<?php echo base_url('getGroupsbyID'); ?>",
            method: 'post',
            data: {"mid": mis},
            success: function (data) {
                $('#pgrouplist').html(data);
                }
             });
        }

        $('#moduleIdentify').change(function(){
            var id = $(this).val();
            console.log(id);
            $.ajax({
            type: "POST",
            url: "<?php echo base_url($this->session->userdata('forajax') . '/searchmodmenu'); ?>",
            data: { "mod_menu_id" : id },
            dataType: 'json',
            success: function(e){
                console.log(e);
                var tmp = "";
                $.each(e, function(i, item) {
                    tmp += "<option value='"+e[i].menu_id+"'>"+e[i].menu_name+"</option>";
                });
                tmp = tmp + "<option value='-1'>None</option>";
                $('#module_menu_option').html(tmp);
                $('select').trigger('chosen:updated');
            }
            });
        });

        $("#prog_parama").validate({
            rules: {
                seq_no: {
                   required: true
                },
                name: {
                   required : true
                },
                desc: {
                   required: true
                },
                data_type: {
                   required: true
                }
            }
        });

        function getparamsa(id){
            $.ajax({
                type: "POST",
                url: "<?php echo base_url($this->session->userdata('forajax') . '/getparams'); ?>",
                data: { "id" :  id },
                dataType: 'json',
                success: function(e){
                    var tmp = '';
                    $.each(e, function(i, item) {
                        tmp += "<tr><td>"+ e[i].seq_no +"</td><td>"+ e[i].name +"</td><td>"+ e[i].desc +"</td><td>"+ e[i].data_type +'</td><td align="center"></td></tr>';
                    });
                    $('#prog_paraam_tbl').html(tmp);
                   //console.log(e);
                }
            });
        }

        $('#saveparama').click(function(){
            if($("#prog_parama").valid() == true && $('#prog_parama input[name=program_id]').val() != null){
                $.ajax({
                    url: "<?php echo base_url($this->session->userdata('forajax') . '/saveparam'); ?>",
                    method:'post',
                    data:$('#prog_parama').serialize(),
                    dataType:'json',
                    success: function (data) {
                        if(data.err == '1'){
                            notifymsg('danger','<b>Error:</b> '+data.msg);
                        }else{
                            notifymsg('success','<b>Success</b>: Data saved.');
                            getparamsa($('#prog_parama input[name=program_id]').val());
                        }
                    },
                    complete: function() {
                        $("#saveparama").removeAttr("disabled");
                    },
                    beforeSend: function(){
                        $("#saveparama").attr("disabled", true);
                    }
                });
                $('#editparam_modal').modal('hide');
            }
        });

        $('#saveProg').click(function(){
            if($('#progForm').valid() == true){
                $.ajax({
                    url: "<?php echo base_url($this->session->userdata('forajax') . '/save'); ?>",
                    method: 'POST',
                    data:$('#progForm').serialize(),
                    dataType:'json',
                    success: function (data) {
                        if(data.err == 1){
                            notifymsg('danger',data.msg);
                        }else{
                            $('#prog_parama').show();
                            $('#prog_parama input[name=program_id]').val(data.modID);
                            notifymsg('success','Save Success!');

                            //close_add();active_search();
                        }
                    },
                    complete: function() {
                        $("#saveProg").removeAttr("disabled");
                    },
                    beforeSend: function(){
                        $("#saveProg").attr("disabled", true);
                    }
                });
                //$('#progForm')[0].reset();
            }
        });

        $("#progForm").validate({
            rules: {
                pcode: {
                    required: true
                },
                pname: {
                    required : true
                },
                modID: {
                    required : true
                },
                ppGroup: {
                    required : true
                },
                pType: {
                    required : true
                }
            }
        });
    });
</script>